<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容">
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <el-table :data="orderList" border stripe>
                <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
                <el-table-column prop="order_number" label="订单编号"></el-table-column>
                <el-table-column prop="order_price" label="订单价格" width="100"></el-table-column>
                <el-table-column prop="pay_status" label="是否付款" width="80">
                    <template v-slot="scope">
                        <el-tag type="primary" v-if="scope.row.order_pay === '1'">已付款</el-tag>
                        <el-tag type="danger" v-else>未付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
                <el-table-column prop="create_time" label="下单时间" width="180">
                    <template v-slot="scope">{{ scope.row.create_time | dateFormat }}</template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template v-slot="scope">
                        <el-button
                            type="primary"
                            size="mini"
                            icon="el-icon-edit"
                            @click="openDialogAddr(scope.row)"
                        ></el-button>
                        <el-button
                            type="warning"
                            size="mini"
                            icon="el-icon-view"
                            @click="openDialogExpress(scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="pageSizes"
                :page-size="queryInfo.pagesize"
                :layout="pageLayout"
                :total="total"
                background
            ></el-pagination>
        </el-card>
        <!-- 修改地址窗口 -->
        <el-dialog
            title="修改地址"
            :visible.sync="addrDialogVisible"
            width="50%"
            @close="addrDialogClose"
        >
            <el-form :model="addrForm" :rules="addrFormRules" ref="addrFormRef" label-width="100px">
                <el-form-item label="省市区/县" prop="address1">
                    <el-cascader
                        v-model="addrForm.address1"
                        :options="cityData"
                        :props="{ expandTrigger: 'hover' }"
                    ></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址" prop="address2">
                    <el-input v-model="addrForm.address2"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addrDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addrSave">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 物流记录窗口 -->
        <el-dialog title="物流记录" :visible.sync="expressDialogVisible" width="50%">
            <el-timeline>
                <el-timeline-item
                    v-for="(item, index) in expressList"
                    :key="index"
                    :timestamp="item.time"
                >{{item.context}}</el-timeline-item>
            </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import config from '@/util/config';
import cityData from '@/util/citydata';
export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: config.pageNum,
                pagesize: config.pageSize
            },
            orderList: [],
            total: 0,
            pageSizes: config.pageSizes,
            pageLayout: config.pageLayout,
            addrDialogVisible: false,
            cityData,
            addrForm: {
                address1: [],
                address2: ''
            },
            addrFormRules: {
                address1: [
                    { required: true, message: '请选择省市区/县', trigger: 'change' }
                ],
                address2: [
                    { required: true, message: '请输入详细地址', trigger: 'blur' }
                ]
            },
            expressList: [],
            expressDialogVisible: false
        }
    },
    created() {
        this.getOrderList()
    },
    methods: {
        // 数据列表
        async getOrderList() {
            const { data: res } = await this.$http.get('orders', {
                params: this.queryInfo
            })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.orderList = res.data.goods
            this.total = res.data.total
        },
        // 监听pagesize
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getOrderList()
        },
        // 监听pagenum
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getOrderList()
        },
        // 打开订单地址窗口
        openDialogAddr(orderInfo) {
            this.addrDialogVisible = true
        },
        // 关闭订单地址窗口
        addrDialogClose() {
            this.$refs.addrFormRef.resetFields()
        },
        // 保存订单地址
        addrSave() {
            this.$refs.addrFormRef.validate(async valid => {
                if (!valid) return
                this.addrDialogVisible = false
            })
        },
        // 物流记录窗口
        async openDialogExpress(orderInfo) {
            const { data: res } = await this.$http.get('/kuaidi/1106975712662')
            if (res.meta.status !== 200) return this.message.error(res.meta.msg)
            this.expressList = res.data
            this.expressDialogVisible = true
        }
    }
}
</script>

<style lang="less" scoped>
.el-cascader {
    width: 100%;
}
</style>